﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Template.Report
{
    internal partial class ReportDesignForm : Form
    {

        private readonly Marmat.Template.Report.Report report;

        public ReportDesignForm(Marmat.Template.Report.Report report)
        {
            InitializeComponent();

            this.report = report;
            this.MinimizeBox = Marmat.Template.Report.ReportConfig.DesignerFormMinimizeBox;
            this.MaximizeBox = Marmat.Template.Report.ReportConfig.DesignerFormMaximizeBox;
            if (!string.IsNullOrEmpty(report.Text))
            {
                this.Text += "报表设计器：" + report.Text;
            }
            // 隐藏关于菜单
            if (this.DesignerControl1.Controls["MainMenu"] is FastReport.Design.StandardDesigner.DesignerMenu menu)
                menu.Items[7].Visible = false;
        }

        private void ReportDesignForm_Load(object sender, EventArgs e)
        {
            //更改设计器的保存报表对话框
            FastReport.Utils.Config.DesignerSettings.CustomSaveReport += this.DesignerSettings_CustomSaveReport;
            FastReport.Utils.Config.DesignerSettings.CustomSaveDialog += this.DesignerSettings_CustomSaveDialog;
            FastReport.Utils.Config.DesignerSettings.CustomPreviewReport += this.DesignerSettings_CustomPreviewReport;

            this.DesignerControl1.Report = this.report.BaseReportObject;
            this.DesignerControl1.RefreshLayout();
        }

        private void DesignerSettings_CustomPreviewReport(object sender, EventArgs e)
        {
            using (ReportPrepareForm frm = new ReportPrepareForm())
            {
                this.report.BaseReportObject.Prepare();
                frm.previewControl1.AddTab(this.report.BaseReportObject, "");
                frm.ShowDialog();
            }
        }

        private void DesignerSettings_CustomSaveDialog(object sender, FastReport.Design.OpenSaveDialogEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.FileName = e.FileName;
            dialog.Filter = "报表文件|*.frx";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                System.IO.File.WriteAllText(dialog.FileName, this.report.SaveToString());
            }
            e.Cancel = true;
        }

        private void DesignerSettings_CustomSaveReport(object sender, FastReport.Design.OpenSaveReportEventArgs e)
        {
            this.report.Save(this.report.SaveToString());
        }

        private void ReportDesignForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //更改设计器的保存报表对话框
            FastReport.Utils.Config.DesignerSettings.CustomSaveReport -= this.DesignerSettings_CustomSaveReport;
            FastReport.Utils.Config.DesignerSettings.CustomSaveDialog -= this.DesignerSettings_CustomSaveDialog;
            FastReport.Utils.Config.DesignerSettings.CustomPreviewReport -= this.DesignerSettings_CustomPreviewReport;
        }
    }
}
